package cn.wxq.daily.empdailymgforserve.controller;
import cn.wxq.daily.empdailymgforserve.domain.Salary;
import cn.wxq.daily.empdailymgforserve.model.Result;
import cn.wxq.daily.empdailymgforserve.model.StatusCode;
import cn.wxq.daily.empdailymgforserve.service.SalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@CrossOrigin //跨越处理
@RestController
@RequestMapping("/salary")
@Api(value = "SalaryController|工资条控制器")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;

    /**
     * 条件查询
     *
     * @param salary
     * @return
     */
    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    @ApiOperation(value = "按条件查询")
    public Result findByPage(Salary salary) {
        List<Salary> res = salaryService.getSalaryByPage(salary);
        if (res != null) {
            return new Result(true, StatusCode.OK, "获取成功", res);
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }

    /**
     * 录入
     *
     * @param salary
     * @return
     */
    @RequestMapping(value = "/addSalary", method = RequestMethod.POST)
    @ApiOperation(value = "录入")
    public Result addSalary(Salary salary) {
        int row = salaryService.addSalary(salary);
        if (row > 0) {
            return new Result(true, StatusCode.OK, "录入成功");
        } else {
            return new Result(false, StatusCode.ERROR, "失败");
        }
    }

    /**
     * 更新
     *
     * @param salary
     * @return
     */
    @RequestMapping(value = "/updateSalary", method = RequestMethod.POST)
    @ApiOperation(value = "更新")
    public Result updateSalary(Salary salary) {
        int row = salaryService.updateSalary(salary);
        if (row > 0) {
            return new Result(true, StatusCode.OK, "更新成功");
        } else {
            return new Result(false, StatusCode.ERROR, "失败");
        }
    }

    /**
     * id获取详情
     *
     * @param adtrtId
     * @return
     */
    @RequestMapping(value = "/getInfoById", method = RequestMethod.POST)
    @ApiOperation(value = "id获取详情", notes = "id获取详情")
    public Result getInfoById(Integer sId) {
        Salary Salary = salaryService.getInfoById(sId);
        if (Salary != null) {
            return new Result(true, StatusCode.OK, "获取用户信息成功", Salary);
        } else {
            return new Result(true, StatusCode.ERROR, "获取用户信息失败");
        }

    }

    /**
     * 删除
     *
     * @param sId
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "删除", notes = "删除")
    public Result deleteById(Integer sId) {
        int row = salaryService.deleteSalary(sId);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
